home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / PROGTOOL / FST30A.ZIP;1 / USELISTS.MOD < prev    next >
Encoding:
Text File  |  1992-09-23  |  6.1 KB  |  247 lines

  1. MODULE UseLists;
  2.  
  3. (* (C) Copyright 1992 Fitted Software Tools. All rights reserved. *)
  4.  
  5. (*
  6.     This module, in conjuction with MyLists and Lists shows examples
  7.     of operations on objects.
  8.  
  9.     The examples are probably not too inspired, but they attempt
  10.     to cover most possibilities.
  11. *)
  12.  
  13. FROM Objects    IMPORT ALLOCATEOBJECT, DEALLOCATEOBJECT, MEMBEROBJECT;
  14. FROM InOut      IMPORT WriteString, WriteLine, WriteCard, Write, WriteLn;
  15. FROM MyLists    IMPORT MyLinkedListItem, MyLinkedList;
  16. IMPORT ASCII;
  17.  
  18.  
  19. (* LOCAL *) CLASS List;
  20.  
  21. (*
  22.     This class is like MyLinkedList.
  23.  
  24.     The only difference is that you get the messages during list
  25.     creation and destruction.
  26. *)
  27.  
  28.     INHERIT MyLinkedList;
  29.  
  30.     INIT
  31.         WriteLine( "<<< Creating a List >>>" );
  32.  
  33.     DESTROY
  34.         WriteLine( "<<< List is being detroyed! >>>" );
  35.  
  36. END List;
  37.  
  38.  
  39.  
  40. (* LOCAL *) CLASS ListItem;
  41.  
  42. (*
  43.     ListItem expands MyLinkedListItem by adding a new attribute (data1) and
  44.     overriding the method print (to print out the contents of data1).
  45.  
  46.     INIT initializes data1 to 0 and DESTROY lets you know what is happening.
  47. *)
  48.  
  49.     INHERIT MyLinkedListItem;
  50.  
  51.     data1 :INTEGER;
  52.  
  53.     PROCEDURE print;    (* override MyLinkedListItem.print *)
  54.     BEGIN
  55.         WriteCard( data1, 1 );
  56.     END print;
  57.  
  58.     PROCEDURE destroyMsg( s :ARRAY OF CHAR );   (* static method *)
  59.     BEGIN
  60.         WriteString( s );
  61.         print;
  62.     END destroyMsg;
  63.  
  64.     INIT
  65.         data1 := 0;
  66.  
  67.     DESTROY
  68.         IF MEMBER( SELF, NewListItem ) THEN
  69.             (* destroying a NewListItem *)
  70.             destroyMsg( " - " );
  71.         ELSE
  72.             destroyMsg( " - destroying ListItem: " );
  73.         END;
  74.  
  75. END ListItem;
  76.  
  77.  
  78.  
  79. (* LOCAL *) CLASS NewListItem;
  80.  
  81. (*
  82.     ListItem expands ListItem by adding a new attribute (data2) and
  83.     overriding the method print (it now prints out the contents of
  84.     data1 + data2).
  85.  
  86.     INIT initializes data2 to 0 and DESTROY lets you know what is happening.
  87. *)
  88.  
  89.     INHERIT ListItem;
  90.  
  91.     data2 :INTEGER;
  92.  
  93.     PROCEDURE print;    (* override ListItem.print *)
  94.     BEGIN
  95.         WriteCard( data1, 1 );
  96.         Write( '+' );
  97.         WriteCard( data2, 1 );
  98.     END print;
  99.  
  100.     (* We know how to append ourselves to a List! *)
  101.     PROCEDURE appendToList( list :List );   (* static method *)
  102.     BEGIN
  103.         list.append( SELF );
  104.     END appendToList;
  105.  
  106.     (* cannot override static method! *)
  107.     (*
  108.     PROCEDURE destroyMsg;
  109.     BEGIN
  110.     END destroyMsg;
  111.     *)
  112.  
  113.     INIT
  114.         (* data1 initialized in ListItem.INIT *)
  115.         data2 := 0;
  116.  
  117.     DESTROY
  118.         destroyMsg( " / destroying NewListItem:" );
  119.  
  120. END NewListItem;
  121.  
  122.  
  123.  
  124. VAR
  125.     aList :List;
  126.     anItem :ListItem;
  127.     aNewItem :NewListItem;
  128.     aBasicItem :MyLinkedListItem;
  129.  
  130. BEGIN
  131.     Write( ASCII.FF );
  132.  
  133.     NEW(aList);
  134.  
  135.     WriteLine( "Adding items (0,1,2,3,4) to aList" );
  136.     NEW(anItem); aList.append(anItem);
  137.     NEW(anItem); anItem.data1 := 1; aList.append(anItem);
  138.     NEW(anItem); anItem.data1 := 2; aList.append(anItem);
  139.     NEW(anItem); anItem.data1 := 3; aList.append(anItem);
  140.     NEW(anItem); anItem.data1 := 4; aList.append(anItem);
  141.  
  142.     WriteLine( "Adding items (1+0,1+1,1+2,1+3,1+4) to aList" );
  143.  
  144.     NEW(aNewItem); aNewItem.data1 := 1;
  145.     aNewItem.appendToList(aList);
  146.  
  147.     NEW(aNewItem); aNewItem.data1 := 1; aNewItem.data2 := 1;
  148.     aNewItem.appendToList(aList);
  149.  
  150.     NEW(aNewItem); aNewItem.data1 := 1; aNewItem.data2 := 2;
  151.     aNewItem.appendToList(aList);
  152.  
  153.     NEW(aNewItem); aNewItem.data1 := 1; aNewItem.data2 := 3;
  154.     aList.append(aNewItem);
  155.  
  156.     NEW(aNewItem); aNewItem.data1 := 1; aNewItem.data2 := 4;
  157.     aList.append(aNewItem);
  158.  
  159.  
  160.     WriteLine( "Items in aList (we will remove 0, 2 and 4):" );
  161.     IF aList.getfirst(anItem) THEN
  162.         REPEAT
  163.             anItem.print;
  164.             IF anItem.data1 MOD 2 = 0 THEN
  165.                 WriteString(' removing ');
  166.                 aList.delete( anItem ); (* remove it from list *)
  167.                 DISPOSE( anItem );      (* and dispose of it *)
  168.             END;
  169.             WriteLn;
  170.         UNTIL NOT aList.getnext(anItem);
  171.     END;
  172.  
  173.     WriteString( "Items remaining in aList:" );
  174.     IF aList.getfirst(anItem) THEN
  175.         REPEAT
  176.             anItem.print;
  177.             Write( ' ' );
  178.         UNTIL NOT aList.getnext(anItem);
  179.     END;
  180.     WriteLn;
  181.  
  182.     WriteString( "ListItem objects: " );
  183.     IF aList.getfirst(aBasicItem) THEN
  184.         REPEAT
  185.             IF MEMBER( aBasicItem, ListItem ) THEN
  186.                 aBasicItem.print;
  187.                 Write( ' ' );
  188.             END;
  189.         UNTIL NOT aList.getnext(aBasicItem);
  190.     END;
  191.     WriteLn;
  192.  
  193.     WriteString( "NewListItem objects (1): " );
  194.     IF aList.getfirst(aBasicItem) THEN
  195.         REPEAT
  196.             IF MEMBER( aBasicItem, NewListItem ) THEN
  197.                 aBasicItem.print;
  198.                 Write( ' ' );
  199.                 (* had we wanted to invoke NewListItem specific methods,
  200.                    we coud assign the object to aNewItem:
  201.                 *)
  202.                 aNewItem := NewListItem(aBasicItem);
  203.             END;
  204.         UNTIL NOT aList.getnext(aBasicItem);
  205.     END;
  206.     WriteLn;
  207.  
  208.     (* not necessarily what it pretends to be... *)
  209.     (*
  210.         List.getfirst and List.getnext take, as a VAR parameter
  211.         any descendant of LinkedListItem. You can get yourself
  212.         in trouble if you pass one of these an object handle of
  213.         a descendant of an item to be returned, as you do, in
  214.         effect, break the type compatibility rules -- a handle
  215.         should never refer to an item which is an ancestor of the
  216.         handle type.
  217.     *)
  218.     WriteString( "NewListItem objects (2): " );
  219.     IF aList.getfirst(aNewItem) THEN
  220.         REPEAT
  221.             IF MEMBER( aNewItem, NewListItem ) THEN
  222.                 aNewItem.print;
  223.                 Write( ' ' );
  224.             ELSE
  225.                 (* we got something that we should not attempt
  226.                    to deal with or we would be breaking the rules...
  227.                    -- we got a ListItem
  228.                 *)
  229.             END;
  230.         UNTIL NOT aList.getnext(aNewItem);
  231.     END;
  232.     WriteLn;
  233.  
  234.     WriteString( "LinkedListItem objects: " );
  235.     IF aList.getfirst(aBasicItem) THEN
  236.         REPEAT
  237.             IF MEMBER( aBasicItem, MyLinkedListItem ) THEN
  238.                 aBasicItem.print;
  239.                 Write( ' ' );
  240.             END;
  241.         UNTIL NOT aList.getnext(aBasicItem);
  242.     END;
  243.     WriteLn;
  244.  
  245.     (* now we dispose of it all *)
  246.     DISPOSE( aList );
  247. END UseLists.